//
//  GWTopicModel.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/3.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWTopicModel.h"
#import "NSDate+Extension.h"
#import <MJExtension.h>
#import "GWComment.h"
#import "GWUser.h"
@implementation GWTopicModel
{
   CGFloat _cellHeight;
}
+(NSDictionary *)mj_replacedKeyFromPropertyName
{
    return @{
             @"smallImage":@"image0",
             @"largeImage":@"image1",
             @"middleImage":@"image2",
             @"ID":@"id"
             };
}
+(NSDictionary *)mj_objectClassInArray
{
    return @{@"top_cmt":[GWComment class]};
}
-(CGFloat)cellHeight
{
    if (!_cellHeight) {
        CGFloat textH =[self.text boundingRectWithSize:CGSizeMake(GWScreenW -GWEssencCellMargin*4, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:0].size.height;
        _cellHeight =GWEssencCellTextY  +textH + GWEssencCellMargin;
        
        if (self.type ==GWTopicControllerTypePicture) {
            CGFloat pictureW =GWScreenW -GWEssencCellMargin*4;
            CGFloat pictureH =pictureW/self.width *self.height;
            if (pictureH>=GWEssencCellPictureMaxH) {
                pictureH =GWEssencCellPictureBreakH;
                self.longPicture =YES;
            }
            _pictureF =CGRectMake(GWEssencCellMargin, _cellHeight, pictureW, pictureH);
            _cellHeight+=(pictureH+GWEssencCellMargin);
            
        }
        else if (self.type ==GWTopicControllerTypeVoice)
        {
            CGFloat voiceW =GWScreenW -GWEssencCellMargin*4;
            CGFloat voiceH =voiceW/self.width *self.height;
            
            _voiceF =CGRectMake(GWEssencCellMargin, _cellHeight, voiceW, voiceH);
            _cellHeight+=(voiceH+GWEssencCellMargin);
        }
        else if (self.type ==GWTopicControllerTypeVideo)
        {
            CGFloat videoW =GWScreenW -GWEssencCellMargin*4;
            CGFloat videoH =videoW/self.width *self.height;
            
            _videoF =CGRectMake(GWEssencCellMargin, _cellHeight, videoW, videoH);
            _cellHeight+=(videoH+GWEssencCellMargin);
        }
        GWComment *cometn =[self.top_cmt firstObject];
        if (cometn) {
            NSString *content =[NSString stringWithFormat:@"%@ : %@",cometn.user.username,cometn.content];
            CGFloat contentH = [content boundingRectWithSize:CGSizeMake(GWScreenW -GWEssencCellMargin*4, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:0].size.height;
            _cellHeight+=(GWEssencCellTopComentTitleH +contentH +GWEssencCellMargin);
        }
        _cellHeight +=(GWEssencCellBottomH+GWEssencCellMargin);
    }
    return _cellHeight;
}
-(NSString *)create_time
{
    
    NSDateFormatter *fm =[[NSDateFormatter alloc]init];
    fm.dateFormat=@"yyyy-MM-dd HH:mm:ss";
    NSDate *currentDate =[NSDate date];
    NSDate *date =[fm dateFromString:_create_time];
    
    if ([date isThisYear]) {//今年
        if ([date isToday]) {//今天
            NSDateComponents *componts =[currentDate componentsFromDtae:date];
            if (componts.hour>=1) {//1以上
                return [NSString stringWithFormat:@"%ld小时前",componts.hour];
            }
            else if (componts.minute>=1)
            {
                return[NSString stringWithFormat:@"%ld分钟前",componts.minute];
            }
            else
            {
                return @"刚刚";
            }
            
        }
        else if ([date isYesterday])
        {//昨天
            fm.dateFormat=@"昨天 HH:mm:ss";
            return [fm stringFromDate:date];
        }
        else
        {
            fm.dateFormat=@"MM-dd HH:mm:ss";
            return [fm stringFromDate:date];
        }
    }
    else
    {//非今年
        return _create_time;
    }
}


@end
